﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.BusinessRules;
using Basic4Net.BusinessRules.View;
using Basic4Net.Controls;
using Basic4Net.Entities;
using Basic4Net.OAWeb.Base;

namespace Basic4Net.OAWeb.Module_Report
{
    public partial class MonthWorkLoadReport :BasePage
    {
        private IList<t_MeritPay> meritPayList = null;
        private IList<t_UnSetWork> setWorkList = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitMonthList();
                Bind(0, aspNetPager.PageSize); 
            }
        }

        private void InitMonthList()
        {
            Amount amountLogic = new Amount();
            IList<string> yearList = amountLogic.GetAmountMonth(SchoolMark, 0);
            if (yearList == null || yearList.Count < 1)
            {
                dropMonth.Items.Add(new ListItem("--当前没有假期工作量数据--", "-1"));
                return;
            }
            dropMonth.DataSource = yearList;
            dropMonth.DataBind();
        }

        private void InitData()
        {
            int year = 0, month = 0;
            string monthValue = dropMonth.SelectedItem.Value.ToString();
            if (!string.IsNullOrEmpty(monthValue))
            {
                if (monthValue.Split('-').Length > 0)
                {
                    string[] arr = monthValue.Split('-');
                    int.TryParse(arr[0], out year);
                    int.TryParse(arr[1], out month);
                }
            }

            string teachCode = TeachCode.Text;

            //获取不固定工作量-项目
            setWorkList = UnSetWork.UnSetWorkInstance.FindAll(SchoolMark);

            //获取综合表信息
            meritPayList =  MeritPay.MeritPayInstance.FindAll(SchoolMark, year, month, teachCode);
        }

        private void Bind(int startAt, int step)
        {
            InitData();

            string teachCode = TeachCode.Text;
         //   int postId = dropList1.SelectedItem.Value == null ? -1 : Convert.ToInt32(dropList1.SelectedItem.Value);
            int count = 0; 
             int userId = Session["UserID"] == null ? 0 : Convert.ToInt32(Session["UserID"]);
            bool existSuper = ExistSuper(userId);
            IList<TeachPostEntity> teacherList = null;
             if (!existSuper)
             {
                 teacherList = PostRelation.PostRelationInstance.GetTeachInfoByPost(startAt, step, null, teachCode,
                                                                                    SchoolMark, ref count, userId);
             }
             else
             {
                 teacherList = PostRelation.PostRelationInstance.GetTeachInfoByPost(startAt, step, null, teachCode,
                                                                                    SchoolMark, ref count, null);
             }


            //teacherList = ((List<TeachPostEntity>)teacherList).FindAll(delegate(TeachPostEntity t)
            //    { return t.TeacherID == userId; });
            //    count = teacherList.Count;
            //}

            TeachCode.Text = "";//清空
            aspNetPager.RecordCount = count;
            Repeater1.DataSource = teacherList;
            Repeater1.DataBind();

        }


        public string GetHeadInfo()
        {
            string str = "<tr>";
            str += "<th rowspan='2' >工号</th> ";
            str += "<th rowspan='2'>姓名</th> ";

            str += "<th colspan='3' >周固定岗位工作量点数B</th> ";

            str += "<th rowspan='2' >周固定社会工作量点数C1</th> ";

            //不固定工作量信息 
            str += "<th colspan='"+setWorkList.Count+"' >月不固定社会工作量C2</th> ";


            str += "<th>其他</th> ";
            str += "</tr>";

            str += "<tr>"; 

            //周固定岗位工作量点数B
            str += "<th >课时</th>";
            str += "<th >学科系数</th>";
            str += "<th >职称系数</th>";
            
           //添加不固定工作量信息 
            foreach (t_UnSetWork item in setWorkList)
            {
                str += "<th>";
                str += item.WorkName;
                str += "</th>";
            }

            str += "<th>减扣课时</th>";
            


            //str += "<th style=\"text-align: left; padding-left: 5px;width: 10%\">减扣课时</th>";
            return str+"</tr>";
        }


        protected  string GetTdInfo(int teacherId)
        {
            string str = "";
            List<t_MeritPay> newList = ((List<t_MeritPay>)meritPayList).FindAll(delegate(t_MeritPay t) { return t.TeacherID == teacherId; });
            if (newList.Count > 0)
            {
                //周固定岗位工作量点数B  课时、学科系数、职称系数
                str += "<td>" + Math.Round(Convert.ToDouble(newList[0].Lesson),1) + "</td>";
                str += "<td>" + Math.Round(Convert.ToDouble(newList[0].Course),1) + "</td>";
                str += "<td>" + Math.Round(Convert.ToDouble(newList[0].Position), 1) + "</td>";

                //周固定社会工作量点数
                str += "<td>" + newList[0].FixedWorkload + "</td>";

                //周固定社会工作量点数
                string notFixedWorkload = newList[0].NotFixedWorkload;
                string[] arr = null;
                if (notFixedWorkload != null && notFixedWorkload.Split(';').Length > 0)
                    arr = notFixedWorkload.Split(';'); 

                foreach (t_UnSetWork item in setWorkList)
                {
                    str += "<td>";
                    if (arr != null && arr.Length > 0)
                    { 
                        foreach (string arrItem in arr)
                        {
                            if (arrItem != null && arrItem.Split(':').Length > 0)
                            {
                                int unWorkId = string.IsNullOrEmpty(arrItem.Split(':')[0]) == true ? 0 : Convert.ToInt32(arrItem.Split(':')[0]);
                                if (unWorkId == item.RID)
                                    str += arrItem.Split(':')[1];
                            }
                        }
                    }
                str += "</td>";
                }
                //减扣课时
                str += "<td>" + Math.Round(Convert.ToDouble(newList[0].Other), 1) + "</td>";
            }
            else
            {
                str += "<td></td>";
                str += "<td></td>";
                str += "<td></td>";

                str += "<td></td>";
                foreach (t_UnSetWork item in setWorkList)
                {
                    str += "<td></td>";
                }
                //str += "<td>" + newList[0].Other + "</td>";
                str += "<td></td>";
            }
            return str;
        }

        protected void btnItemBind_Click(object sender, EventArgs e)
        {
            Bind((aspNetPager.PageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }

        /// <summary>
        /// 分页控件事件
        /// </summary>
        protected void AspNetPagerPageChanged(object sender, BNPager.PageChangedEventArgs e)
        {
            TeachCode.Text = "";//清空
            if (e.NewPageIndex <= 0)
                return;
            aspNetPager.PageIndex = e.NewPageIndex;
            Bind((e.NewPageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }

        /// <summary>
        /// 月份下拉事件
        /// </summary>
        protected void DrMonthChanged(object sender, EventArgs e)
        {
            Bind(0, aspNetPager.PageSize);
        }
    }
}